<?php

namespace app\api\controller;

use think\facade\Cache;
use app\common\model\Member as ModelMember;
use app\common\model\MemberLevel;
use think\facade\Request;

class Member
{
    /*用户登录注册*/
    public function Login()
    {
        if (empty(input('type'))) {
            return BackCode([], 203, '登录方式不能为空');
        }
        $ModelMember = new ModelMember();
        $member = [];
        switch (input('type')) {
            case "user":
                if (empty(input('username'))) {
                    return BackCode([], 203, '账号不能为空');
                }
                if (empty(input('userpass'))) {
                    return BackCode([], 203, '密码不能为空');
                }
                $where="is_delete=0 and (user='" . input('username') . "' or phone='" . input('username') . "' or email='" . input('username') . "') and password='" . md5(input('userpass')) . "'";
                $member = $ModelMember->where($where)->find();
                if (empty($member)) {
                    return BackCode([], 203, '账号密码错误');
                }
                if ($member['status'] != 1) {
                    return BackCode([], 203, '账号已停用');
                }

                break;
            case "phone":
            case "email":
                if (empty(input('number'))) {
                    return BackCode([], 203, '号码不能为空');
                }
                if (empty(input('code'))) {
                    return BackCode([], 203, '验证码不能为空');
                }
                $CacheId = input('type') . '_' . input('number');
                $code = Cache::get($CacheId);
                if ($code != input('code')) {
                    return BackCode([], 203, '验证码与登录号码不匹配');
                }
                $field = "phone";
                if (input('type') == 'email') {
                    $field = "email";
                }
                $where = "is_delete=0 and " . $field . "='" . input('number') . "'";
                $member = $ModelMember->where($where)->find();
                $status = !empty($member['status']) ? $member['status'] : 1;
                if ($status != 1) {
                    return BackCode([], 203, '账号已停用');
                }
                if (empty($member)) {
                    $MemberLevel = new MemberLevel();
                    $Level = $MemberLevel->where("is_delete=0 and status=1")->field("id")->order("sort asc")->find();
                    $params[$field] = input('number');
                    $params['user'] = time();
                    $params['level_id'] = $Level['id'];
                    $params['password'] = 123456;
                    $params['gender'] = 3;
                    $Logic = new \app\member\service\user\Logic();
                    $result = $Logic->Add($params);
                    if ($result['code'] != 200) {
                        return BackCode([], $result['code'], $result['msg']);
                    }
                    $member = $result['data'];
                }
                break;
        }
        if (!empty($member)) {
            $ip = GetIpText();
            $upData = [
                'login_time' => date('Y-m-d H:i:s'),
                'login_ip' => !empty($ip['ip']) ? $ip['ip'] : Request::ip(),
                'login_ip_address' => !empty($ip['address']) ? str_replace(" ", "", $ip['address']) : '内部地址',
            ];
            $ModelMember->where("id='" . $member['id'] . "'")->update($upData);
            $find['user_info'] = $ModelMember->where("id='" . $member['id'] . "'")->withoutField("password")->find();
            $find['user_info']['login_source'] = "user";
            $find['user_token'] = signToken($find['user_info'], 3600 * 24);
            return BackCode($find);
        }
        return BackCode([], 203, '登录失败');
    }


    /*编辑用户信息*/
    public function Edit()
    {

    }

    /*用户信息*/
    public function Info()
    {
        $source = GetToken("login_source");
        if ($source != 'user') {
            return BackCode([], 203, '未登录');
        }
        $id = GetToken("id");
        $ModelMember = new ModelMember();
        $find = $ModelMember->where("id='" . $id . "'")->withoutField("password")->find();
        return BackCode($find);
    }

    /*发送验证码*/
    public function GetCode()
    {
        if (empty(input('type'))) {
            return BackCode([], 203, '登录方式不能为空');
        }
        if (empty(input('number'))) {
            return BackCode([], 203, '号码不能为空');
        }
        $IpCacheId = "code_" . md5(Request::ip());
        $CacheId = input('type') . '_' . input('number');
        $code = Cache::get($CacheId);
        $IpNum = Cache::get($IpCacheId) ?: 0;
        if ($IpNum > 10) {
            return BackCode([], 203, '请不要频繁获取验证码');
        }
        if (empty($code)) {
            $IpNum++;
            Cache::set($IpCacheId, $IpNum, 3600);
            $code = mt_rand(100000, 999999);
            Cache::set($CacheId, $code, 300);
            $thatSend = new \app\api\service\sms\Send();
            $onlyNum = '';
            switch (input('type')) {
                case "phone":
                    $onlyNum = 'phone_login_reg';
                    break;
                case "email":
                    $onlyNum = 'email_login_reg';
                    break;
            }
            $result = $thatSend->Sms($onlyNum, input('number'), ['code' => $code]);
            if ($result['code'] == 2) {
                return BackCode([], 200, '验证码发送成功，5分钟内有效');
            } else {
                return BackCode([], 203, '验证码发送失败');
            }
        }
        return BackCode([], 203, '5分钟内不可重复获取');
    }


}